var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vouType: 1,
    isIphoneX: 0,
    page: 1,
    size: 8,
    allPage: 1,
    count: 0,
    isMore:0,
    vouchersList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取设备机型信息
    let phone = wx.getStorageSync('isIphoneX');
    if (phone) {
      this.setData({
        isIphoneX: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取设备信息
    let info = wx.getSystemInfoSync();
    let sysHeight = info.windowHeight - 100;
    this.setData({
      sysHeight: sysHeight,
      isMore:0,
      vouchersList:[],
      allPage: 1,
      count: 0,
      page: 1,
      size: 8,
    });
    // 获取券的列表
    this.getUserVouchers(this.data.vouType);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.page >= that.data.allPage) {
        that.setData({
          isMore: 1
        });
        return false;
    }
    that.setData({
        page: that.data.page + 1
    });
    that.getUserVouchers(that.data.vouType);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 切换标签页
  switchTab: function (event) {
    let vouType = event.currentTarget.dataset.index;
    this.setData({
      page:1,
      allPage: 1,
      count: 0,
      isMore:0,
      vouType: vouType,
      vouchersList: [],
    });
    this.getUserVouchers(vouType);
  },
  // 获取抵用券列表
  getUserVouchers(vouType) {
    let that = this
    let userID = wx.getStorageSync('userID')
    util.requestApi(api.getUserVouchers, {
      userID: userID,
      page: that.data.page,
      size: that.data.size,
      status:vouType
    }).then(function (res) {
      if (res.code == 200) {
        that.setData({
          vouchersList: that.data.vouchersList.concat(res.content.data),
          count: res.content.count,
          allPage: Math.ceil(res.content.count / that.data.size),
          isMore:res.content.data.length < that.data.size ? 1 : 0,
        });
      }
    })
  },
  goIndex(){
    wx.switchTab({
      url: '/pages/hot-goods/index',
    });
  }
})